package at.tuwien.seminar.scrum1.view.wicket.reservation;

import java.util.Date;

import at.tuwien.seminar.scrum1.view.error.ProblemPage;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import at.tuwien.seminar.scrum1.model.Reservation;
import at.tuwien.seminar.scrum1.proxies.ReservationProxy;
import at.tuwien.seminar.scrum1.view.reservation.list.ReservationList;

public class ReservationDeleteActionPanel extends Panel
{
    /**
     * @param id
     *            component id
     * @param model
     *            model for contact
     */
    public ReservationDeleteActionPanel(String id, final IModel<Reservation> model)
    {
        super(id, model);
        add(new Link("select")
        {
            @Override
            public void onClick()
            {
            	Reservation reservation = model.getObject();
            	
            	// reservation can be deleted
            	if(reservation.getStartDate().getTime() > new Date().getTime())
            	{
            		ReservationProxy.getInstance().deleteReservation(reservation);
                    setResponsePage(new ReservationList(ReservationProxy.getInstance().getAll()));
            	}
            	else
            		setResponsePage(new ProblemPage(reservation));
            }
        });
    }
}
